<template>
  <element1x1 :pop="pop"
              v-loading="loading"
              :element-loading-text="loadingText"
              element-loading-spinner="el-icon-warning-outline"
              element-loading-background="rgba(0, 0, 0, 0.6)">
    <slot slot="close"></slot>
    <div class="con" slot="content">
      <h3>{{sumEnt.data}}%</h3>
      <p class="sum-p">
        <span class="leftspan" >年同比</span>
        <span class="rightspan">
            <i :class="sumEnt.tb<0?'el-icon-caret-bottom':(!!sumEnt.tb?'el-icon-caret-top':'el-icon-minus')"></i>
            {{sumEnt.tb==null||sumEnt.tb===''?'':Math.abs(sumEnt.tb)}}%
          </span>
        <span  class="leftspan" >环比</span>
        <span  class="rightspan">
            <i
              :class="sumEnt.hb<0?'el-icon-caret-bottom':(!!sumEnt.hb?'el-icon-caret-top':'el-icon-minus')"></i>
             {{sumEnt.hb==null||sumEnt.hb===''?'':Math.abs(sumEnt.hb)}}%
          </span>
      </p>
    </div>

  </element1x1>
</template>
<style lang="scss">

</style>
<script>
  let moment = require("moment");
  import element1x1 from "@/components/real-time/real-time-module/common/size/Element1x1";
  export default {
    components: {
      element1x1:element1x1
    },
    props: {
      GetData:{},
      isdemo:{
        type:Boolean,
        default:function(){
          return false
        }
      }
    },
    data(){
      return {
        pop:{
          title: "折扣率",							//报表标题
          helphint:[
            {title:"折扣率",text:""},				//第一条信息为提示标题 不给text
            {title:"折扣率",text:"（销售金额/原价金额）*100"},				//第一条信息为提示标题 不给text
          ]
        },
        realData:{
          company_ids:'',//企业id
          case_ids:'',//案场id
          idsType:0,//企业或案场标记：0：为只选择了企业id，1：为选择了案场id
          start_day:moment(new Date()).format("YYYY-MM-DD"),
          end_day:moment(new Date()).format("YYYY-MM-DD"),
        },
        loading:false,
        loadingText:'加载中...',
        start_hm:'00:00',
        end_hm:'23:59',
        global:null,
        org_type:null,
        xykpis:['discount_avg_rate'],
        sumEnt:{
          data:'0',
          hb:'',
          tb:''
        },
        shake:false
      }
    },
    created() {
      if(this.isdemo){
        this.sumEnt={
          data:'66.8',
          hb:'10',
          tb:'12'
        };
        this.loading=false;
      }
    },
    mounted() {
    },
    beforeDestroy(){},
    methods: {
      getData(){
        let data={};
        let tags=0;
        if(this.global.type=='chain')
        {
          tags=2;
        }
        else if(this.global.type=='mall')
        {
          tags=1;
        }
        let group_type='';
        if(this.realData.idsType){ //0企业 1案场
          data = {
            org_id_list : this.realData.case_ids.split(","),
            org_type:2,
          }
          group_type='case';
        }
        else{
          if(this.org_type == 1){
            data = {
              org_id_list : this.realData.company_ids.split(","),
              org_type:1,
            }
            group_type='company';
          }else{
            data = {
              org_id_list : this.realData.allCase_ids,
              org_type:2,
            }
            group_type='case';
          }

        }           //接口调用指标能少就少，后台速度会快一些
        let url = '/sys/report_new/distribution_sum_ratio?group_type='+group_type+'&tags='+tags+'&indicator_keys='+this.xykpis+
          '&huanbi_type=day&tongbi_type=year&start_day=' + this.realData.start_day + '&end_day='+ this.realData.end_day +
          '&start_hm='+ this.start_hm + '&end_hm=' + this.end_hm;
        this.$api.post(url,data)
        .then(data=>{
          if(data == '10001'){
            this.removeData();
            this.loadingText = '无该指标权限';
            this.shake = false
            return
          }
          if(data&&data.data.sum_list.length)
          {
            this.sumEnt={
              data:data.data.sum_list[0].discount_avg_rate,
              hb:data.data.sum_list[0].discount_avg_rate_hb,
              tb:data.data.sum_list[0].discount_avg_rate_tb
            }
            if(this.realData.end_day==moment(new Date()).format("YYYY-MM-DD"))
            {
              this.sumEnt.hb='';
              this.sumEnt.tb='';
            }
            this.$nextTick(()=>{
              this.loading = false;
            })
          }
          else{
            this.removeData();
            this.loadingText = '无数据';
          }
          this.shake = false
        })
        .catch(err=>{
          this.removeData();
          this.loadingText = '无数据';
          this.shake = false
        })
      },
      removeData(){
        this.sumEnt={
          data:'0',
          hb:'',
          tb:''
        }
      },
    },
    watch: {
      GetData:{
        handler(val){
          if(this.shake && val.index != 0){
            return
          }
          if(val){
            this.realData = val.realHeaderData;
            this.global = val.global;
            this.org_type = val.org_type;
            this.loading = true;
            this.shake = true
            if(!this.realData.company_ids.length){
              this.removeData();
              this.loadingText = '无数据';
              this.shake = false
              return
            }else{
              this.loadingText = '加载中...';
              this.getData();
            }
          }
        },
        deep:true
      }
    },

  };
</script>
